package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfGoodsClassDAO;
import com.ljkj.common.db.dao.ZjfGoodsDAO;
import com.ljkj.common.db.domain.ZjfGood;
import com.ljkj.common.db.domain.ZjfGoodsClass;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfGoodService {
	
	private ZjfGoodsDAO zjfGoodsDAO;
	private TLogService tLogService;
	private ZjfGoodsClassService zjfGoodsClassService;


	/**
	 * 获取产品
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getGoodsByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfGood.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = {"goodsName","goodsDes","goodsDetails"};
		Object[] likevalues= {params,params,params};
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfGoodsDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfGoodsDAO.setPageMaxCount(pageSize);
		List<ZjfGood> zjfGoods = this.zjfGoodsDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfGood zjfGood : zjfGoods){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("goodsId",zjfGood.getGoodsId());
			entry_map.put("goodsName",zjfGood.getGoodsName());
			entry_map.put("goodsDes",zjfGood.getGoodsDes());
			entry_map.put("goodsPrice",zjfGood.getGoodsPrice());
			entry_map.put("goodsStorePrice",zjfGood.getGoodsStorePrice());
			entry_map.put("goodsInventory",zjfGood.getGoodsInventory());
			entry_map.put("goodsSalenum",zjfGood.getGoodsSalenum());
			entry_map.put("goodsSerial",zjfGood.getGoodsSerial());
			entry_map.put("goodsDetails",zjfGood.getGoodsDetails());
			entry_map.put("goodsPackDetails",zjfGood.getGoodsPackDetails());
			entry_map.put("goodsService",zjfGood.getGoodsService());
			entry_map.put("goodsRecommend",zjfGood.getGoodsRecommend());
			entry_map.put("goodsClick",zjfGood.getGoodsClick());
			entry_map.put("goodsCollect",zjfGood.getGoodsCollect());
			entry_map.put("goodsCod",zjfGood.getGoodsCod());
			entry_map.put("goodsStatus",zjfGood.getGoodsStatus());
			entry_map.put("goodsShelvesTime",zjfGood.getGoodsShelvesTime());
			entry_map.put("goodsTransfee",zjfGood.getGoodsTransfee());
			entry_map.put("goodsMainPhoto",zjfGood.getGoodsMainPhoto());
			entry_map.put("goodsActivityStatus",zjfGood.getGoodsActivityStatus());
			entry_map.put("goodsActivityId",zjfGood.getGoodsActivityId());
			entry_map.put("goodsCliffordStatus",zjfGood.getGoodsCliffordStatus());
			entry_map.put("goodsDesImg",zjfGood.getGoodsDesImg());
			entry_map.put("goodsClassId",zjfGood.getGoodsClassId());
			if(zjfGood.getGoodsClassId() != null &&!"".equals(zjfGood.getGoodsClassId())){
				ZjfGoodsClass zjfGoodsClass = this.zjfGoodsClassService.getGoodsClassById(zjfGood.getGoodsClassId());
				if(zjfGoodsClass != null){
					entry_map.put("goodsClassName",zjfGoodsClass.getClassName());
				}
			}
			
			entry_map.put("createTime",zjfGood.getCreateTime());
			entry_map.put("createUser", zjfGood.getCreateUser());
			entry_map.put("updateTime", zjfGood.getUpdateTime());
			entry_map.put("updateUser", zjfGood.getUpdateUser());
			entry_map.put("disabledState", zjfGood.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
//	public String getGoodsClassForSonClass(){
//
//		List<ZjfGoodsClass> zjfGoodsClasses = this.zjfGoodsClassDAO.findByProperty("disabledState", 0);
//		
//		List<Object> entry_list = new ArrayList<Object>();
//		//迭代遍历所有条目，放到map里
//		for(ZjfGoodsClass zjfGoodsClass : zjfGoodsClasses){
//			
//			Map<String,Object> entry_map = new HashMap<String,Object>();
//			entry_map.put("classId",zjfGoodsClass.getClassId());
//			entry_map.put("className", zjfGoodsClass.getClassName());
//			
//			entry_list.add(entry_map);
//		}
//		
//        return JSON.toJSONString(entry_list);
//	}
	
	
	
	/**
	 * 新增或更新商品
	 * @param zjfGoodsClass
	 * @return
	 */
	public String addOrEditGoodInfo(ZjfGood zjfGood,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfGood.getGoodsId() == null){
			
			
			tlog.setOpType(1);
			tlog.setContent("新增商品："+ zjfGood.getGoodsName());
			zjfGood.setGoodsId(RandomCode.makeCode());
			zjfGood.setCreateTime(DateTime.getDateTime());
			zjfGood.setCreateUser(tuser.getLoginName());
			if(this.saveNewGood(zjfGood)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfGood.getDisabledState()!=null && zjfGood.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("商品状态："+zjfGood.getGoodsName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改商品："+zjfGood.getGoodsName());
				}
				
				zjfGood.setUpdateTime(DateTime.getDateTime());
				zjfGood.setUpdateUser(tuser.getLoginName());
				this.zjfGoodsDAO.update(zjfGood);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewGood(ZjfGood zjfGood){
		try{
			this.zjfGoodsDAO.save(zjfGood);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfGood getGoodById(String goodId){
		return this.zjfGoodsDAO.findById(goodId);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

	public void setZjfGoodsDAO(ZjfGoodsDAO zjfGoodsDAO) {
		this.zjfGoodsDAO = zjfGoodsDAO;
	}

	public void setZjfGoodsClassService(ZjfGoodsClassService zjfGoodsClassService) {
		this.zjfGoodsClassService = zjfGoodsClassService;
	}

}
